package com.example.recuitactivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.bean.Collection;
import com.example.recuitactivity.bean.Deliver;
import com.example.recuitactivity.bean.RecuitInfo;
import com.example.recuitactivity.bean.Resume;
import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.helper.TimeHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ViewRecruitInfoActivity extends AppCompatActivity {
    private TextView tv_name, tv_company, tv_salary, tv_location, tv_edubg, tv_detail, tv_scale;
    private Button btn_apply, btn_cancel;
    private ImageButton ib_back, ib_collect;
    private DbOpenHelper dbOpenHelper;
    private String username;
    private long pid;//岗位id
    private Boolean star=false;//默认为未收藏

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_recruit_apply:
                    if (!checkResumeCompleted()) {
                        new AlertDialog.Builder(ViewRecruitInfoActivity.this)
                                .setTitle("提示")
                                .setMessage("请先完善您的简历信息")
                                .setPositiveButton("确定", null)
                                .show();
                    } else {
                        new AlertDialog.Builder(ViewRecruitInfoActivity.this)
                                .setTitle("提示")
                                .setMessage("确定投递该岗位吗？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        deliverResume();
                                        Toast.makeText(ViewRecruitInfoActivity.this, "投递成功", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }
                                })
                                .setNegativeButton("取消", null)
                                .show();
                    }
                    break;
                case R.id.btn_recruit_cancel:
                    new AlertDialog.Builder(ViewRecruitInfoActivity.this)
                            .setTitle("提示")
                            .setMessage("确定取消该岗位的投递吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    cancelDeliver();
                                    Toast.makeText(ViewRecruitInfoActivity.this, "已取消", Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            })
                            .setNegativeButton("取消", null)
                            .show();
                    break;
            }
        }
    };

    private View.OnClickListener listener2 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.ib_back:
                    finish();
                    break;
                case R.id.ib_collection:
                    long cid=dbOpenHelper.getCandidateIdByUsername(username);
                    Toast toast=Toast.makeText(ViewRecruitInfoActivity.this,"",Toast.LENGTH_SHORT);
                    if(star){
                        dbOpenHelper.deleteCollects(cid,pid);
                        star=false;
                        ib_collect.setImageResource(R.drawable.uncollect);
                        toast.setText("取消收藏成功");

                    }else{
                        dbOpenHelper.insertCollects(new Collection(cid,pid));
                        star=true;
                        ib_collect.setImageResource(R.drawable.collected);
                        toast.setText("收藏成功");
                    }
                    toast.setGravity(Gravity.CENTER,0,0);
                    toast.show();
            }
        }
    };

    public void initUI() {
        tv_name = (TextView) findViewById(R.id.tv_view_recruit_name);
        tv_company = (TextView) findViewById(R.id.tv_view_recruit_company);
        tv_salary = (TextView) findViewById(R.id.tv_view_recruit_salary);
        tv_location = (TextView) findViewById(R.id.tv_view_recruit_location);
        tv_edubg = (TextView) findViewById(R.id.tv_view_recruit_edubg);
        tv_detail = (TextView) findViewById(R.id.tv_view_recruit_detail);
        tv_scale = (TextView) findViewById(R.id.tv_view_recruit_scale);
        btn_apply = (Button) findViewById(R.id.btn_recruit_apply);
        btn_cancel = (Button) findViewById(R.id.btn_recruit_cancel);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        ib_collect = (ImageButton) findViewById(R.id.ib_collection);
        btn_apply.setOnClickListener(listener);
        btn_cancel.setOnClickListener(listener);
        ib_back.setOnClickListener(listener2);
        ib_collect.setOnClickListener(listener2);
    }

    public void initDatas() {
        dbOpenHelper = new DbOpenHelper(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String action = intent.getAction();
        username = bundle.getString("username");
        if (action.equals("just_view")) {
            btn_apply.setVisibility(View.INVISIBLE);
            btn_cancel.setVisibility(View.VISIBLE);
            long cid = dbOpenHelper.getCandidateIdByUsername(username);
            pid = bundle.getLong("pid");
            Log.d("tag",String.valueOf(pid));
            RecuitInfo recuitInfo = dbOpenHelper.getRecruitInfoById(pid);

            star=dbOpenHelper.isExistCollects(cid,pid);
            if(star){
                ib_collect.setImageResource(R.drawable.collected);
            }else{
                ib_collect.setImageResource(R.drawable.uncollect);
            }

            tv_name.setText(recuitInfo.getRecuitName());
            tv_company.setText(recuitInfo.getEnterpriseName());
            tv_salary.setText(recuitInfo.getSalary());
            tv_location.setText(recuitInfo.getLocation());
            tv_edubg.setText(recuitInfo.getEduRequest());
            tv_detail.setText(recuitInfo.getDetail());
            tv_scale.setText(recuitInfo.getScale());
        } else if (action.equals("EnterpriseAllinfoFragment")) {
            btn_apply.setVisibility(View.INVISIBLE);
            btn_cancel.setVisibility(View.INVISIBLE);
            ib_collect.setVisibility(View.INVISIBLE);

            //填充岗位信息
            tv_name.setText(bundle.getString("recruit_name"));
            tv_company.setText(bundle.getString("enterprise_name"));
            tv_salary.setText(bundle.getString("salary"));
            tv_location.setText(bundle.getString("location"));
            tv_edubg.setText(bundle.getString("edubg_request"));
            tv_detail.setText(bundle.getString("detail"));
            tv_scale.setText(bundle.getString("scale"));
        } else if (action.equals("CandidateInfoFragment")||action.equals("CollectionActivity")) {
            btn_apply.setVisibility(View.VISIBLE);
            btn_cancel.setVisibility(View.INVISIBLE);

            //判断该岗位是否已经投递，已投递则改变按钮样式
            long cid = dbOpenHelper.getCandidateIdByUsername(username);
            pid = bundle.getLong("id");
            long eid = dbOpenHelper.getEnterpriseIdByCompany(bundle.getString("enterprise_name"));
            String state = dbOpenHelper.getStateByCidAndEidAndPid(cid, eid, pid);
            star=dbOpenHelper.isExistCollects(cid,pid);
            if(star){
                ib_collect.setImageResource(R.drawable.collected);
            }else{
                ib_collect.setImageResource(R.drawable.uncollect);
            }
            if (!state.equals("")) {
                btn_apply.setText(state);
                btn_apply.setClickable(false);
                btn_apply.setBackgroundColor(getResources().getColor(R.color.grey));
            }

            //填充岗位信息
            tv_name.setText(bundle.getString("recruit_name"));
            tv_company.setText(bundle.getString("enterprise_name"));
            tv_salary.setText(bundle.getString("salary"));
            tv_location.setText(bundle.getString("location"));
            tv_edubg.setText(bundle.getString("edubg_request"));
            tv_detail.setText(bundle.getString("detail"));
            tv_scale.setText(bundle.getString("scale"));
            if(bundle.getBoolean("unread")==true){
                new AlertDialog.Builder(ViewRecruitInfoActivity.this)
                        .setTitle("温馨提示")
                        .setMessage("该岗位有信息修改！")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        })
                        .show();
                if(dbOpenHelper.getNoScanRecordsCountByCid(cid)==0){
                    dbOpenHelper.updateCandidateFlagByCid(cid,0);
                }
            }
        }
    }


    public void deliverResume() {
        Bundle bundle = getIntent().getExtras();
        String recruit_name = bundle.getString("recruit_name");
        long cid = dbOpenHelper.getCandidateIdByUsername(username);
        long pid = bundle.getLong("id");
        long eid = dbOpenHelper.getEnterpriseIdByCompany(bundle.getString("enterprise_name"));
        String time = TimeHelper.getInstance().getCurrentTime();
        String state = "已投递";
        dbOpenHelper.insertDeliver(new Deliver(cid, eid, pid, recruit_name, time, state));
    }

    public void cancelDeliver() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        long cid = bundle.getLong("cid");
        long eid = bundle.getLong("eid");
        long pid = bundle.getLong("pid");
        dbOpenHelper.deleteDeliverByCidAndEidAndPid(cid, eid, pid);
    }

    public boolean checkResumeCompleted() {
        long id = dbOpenHelper.getCandidateIdByUsername(username);
        Resume resume = dbOpenHelper.getResumeInfoById(id);
        if (resume.getName().length() == 0 || resume.getSex().length() == 0 || resume.getEdu_background().length() == 0 || resume.getAge() == 0 ||
                resume.getTelephone().length() == 0 || resume.getEdu_background().length() == 0 || resume.getProject_exp().length() == 0 || resume.getReward_exp().length() == 0) {
            return false;
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_recruit_info);
        initUI();
        initDatas();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return false;
    }

}